package com.bocai.grainshop.cart.web;

import com.bocai.common.utils.R;
import com.bocai.grainshop.cart.service.CartService;
import com.bocai.grainshop.cart.vo.CartItemVo;
import com.bocai.grainshop.cart.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping("/success")
    public String success(){
        return "success";
    }

    @GetMapping("/cart")
    public String cart(Model model){
        CartVo cartVo = cartService.getCart();
        model.addAttribute("cart",cartVo);
        return "cartList";
    }

    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num, RedirectAttributes redirectAttributes){
        cartService.addToCart(skuId,num);
        redirectAttributes.addAttribute("skuId",skuId);
        return "redirect:http://cart.grainshop.com/addToCartSuccess";
    }

    /**
     * @Author: SunJinbo
     * @Description  解决重复提交
     * @Param [skuId, model]
     * @return java.lang.String
     * @Data Created in 2022/4/17 18:07
     */

    @GetMapping("/addToCartSuccess")
    public String addToCartSuccess(@RequestParam("skuId") Long skuId,Model model){
        CartItemVo cartItemVo = cartService.getItemMessage(skuId);
        model.addAttribute("item",cartItemVo);
        return "success";
    }

    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("checked") Integer checked){
        cartService.checkItem(skuId,checked);
        return "redirect:http://cart.grainshop.com/cart";
    }

    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num){
        cartService.countItem(skuId,num);
        return "redirect:http://cart.grainshop.com/cart";
    }

    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId){
        cartService.deleteItem(skuId);
        return "redirect:http://cart.grainshop.com/cart";
    }

    @ResponseBody //对象没有序列化  就传json  序列化就不必
    @GetMapping("/getUserCartItems")
    public List<CartItemVo> getUserCartItems(){
        return cartService.getUserCartItems();
    }

    @ResponseBody
    @GetMapping("/update/cartItems")
    public R updateCartItems(){
        try{
            cartService.updateCartItems();
            return R.ok();
        }catch (Exception e){
            return R.error(1111,"修改购物车错误！");
        }
    }
}
